package com.yjtc.cbg.commonutildemo.View;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yjtc.cbg.commonutildemo.R;
import com.yjtc.cbg.commonutildemo.Util.XmlParserHandler;
import com.yjtc.cbg.commonutildemo.entity.CityModel;
import com.yjtc.cbg.commonutildemo.entity.DistrictModel;
import com.yjtc.cbg.commonutildemo.entity.ProvinceModel;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by chenboge on 16/9/4.
 */
public class PickerActivity extends BaseActivity implements View.OnClickListener {


    protected String mCurrentProviceName;
    /**
     */
    protected String mCurrentCityName;
    /**
     */
    protected String mCurrentDistrictName = "";

    protected String mCurrentZipCode = "";


    private List<ProvinceModel> mProvinces = new ArrayList<>();
    private ArrayList<ArrayList<String>> mCities = new ArrayList<ArrayList<String>>();
    private ArrayList<ArrayList<ArrayList<String>>> mDistricts = new ArrayList<ArrayList<ArrayList<String>>>();

    private TextView pickerText;

    protected String[] mProvinceDatas;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_picker);
        pickerText = (TextView) findViewById(R.id.picker);
        pickerText.setOnClickListener(this);


    }

    @Override
    public int getContentVIew() {
        return 0;
    }

    @Override
    public void onClick(View v) {
    }

    protected void initProvinceDatas()
    {
        List<ProvinceModel> provinceList = null;
        AssetManager asset = getAssets();
        try {
            InputStream input = asset.open("province_data.xml");
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser parser = spf.newSAXParser();
            XmlParserHandler handler = new XmlParserHandler();
            parser.parse(input, handler);
            input.close();
            provinceList = handler.getDataList();
            mProvinces = provinceList;
            if (provinceList!= null && !provinceList.isEmpty()) {
                mCurrentProviceName = provinceList.get(0).getName();
                List<CityModel> cityList = provinceList.get(0).getCityList();
                if (cityList!= null && !cityList.isEmpty()) {
                    mCurrentCityName = cityList.get(0).getName();
                    List<DistrictModel> districtList = cityList.get(0).getDistrictList();
                    mCurrentDistrictName = districtList.get(0).getName();
                    mCurrentZipCode = districtList.get(0).getZipcode();
                }
            }
            //*/
            mProvinceDatas = new String[provinceList.size()];
            for (int i=0; i< provinceList.size(); i++) {
                mProvinceDatas[i] = provinceList.get(i).getName();
                List<CityModel> cityList = provinceList.get(i).getCityList();
                ArrayList<String> citys = new ArrayList<>();
                String[] cityNames = new String[cityList.size()];
                for (int j=0; j< cityList.size(); j++) {
                    citys.add(cityList.get(j).getName());
                    cityNames[j] = cityList.get(j).getName();
                    List<DistrictModel> districtList = cityList.get(j).getDistrictList();
                    ArrayList<ArrayList<String>> dts = new ArrayList<>(); // 地区 List
                    String[] distrinctNameArray = new String[districtList.size()];
                    DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];

                    ArrayList<String> districtStrs = new ArrayList<>();

                    for (int k=0; k<districtList.size(); k++) {
                        districtStrs.add(districtList.get(k).getName());
//                        DistrictModel districtModel = new DistrictModel(districtList.get(k).getName(), districtList.get(k).getZipcode());
//                        mZipcodeDatasMap.put(districtList.get(k).getName(), districtList.get(k).getZipcode());
//                        distrinctArray[k] = districtModel;
//                        distrinctNameArray[k] = districtModel.getName();
                    }

                    dts.add(districtStrs);

                    mDistricts.add(dts);
                    //mDistrictDatasMap.put(cityNames[j], distrinctNameArray);
                }
                mCities.add(citys);
                //mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {

        }
    }

//    protected void initProvinceDatas() {
//
//        AssetManager asset = getAssets();
//        try {
//            InputStream input = asset.open("province_data.xml");
//            // 创建一个解析xml的工厂对象
//            SAXParserFactory spf = SAXParserFactory.newInstance();
//            // 解析xml
//            SAXParser parser = spf.newSAXParser();
//            XmlParserHandler handler = new XmlParserHandler();
//            parser.parse(input, handler);
//            input.close();
//            // 获取解析出来的数据
//            mProvinces = handler.getDataList();
//
//        } catch (Throwable e) {
//            e.printStackTrace();
//        } finally {
//
//        }
//        if (mProvinces != null) {
//
//            for (ProvinceModel p : mProvinces) {
//
//                List<CityModel> cities = p.getCityList();
//                ArrayList<String> cityStrs = new ArrayList<>(cities.size()); //城市List
//
//
//                for (CityModel c : cities) {
//
//                    cityStrs.add(c.getName()); // 把城市名称放入 cityStrs
//
//
//                    ArrayList<ArrayList<String>> dts = new ArrayList<>(); // 地区 List
//
//                    List<DistrictModel> districts = c.getDistrictList();
//                    ArrayList<String> districtStrs = new ArrayList<>(districts.size());
//
//                    for (DistrictModel d : districts) {
//                        districtStrs.add(d.getName()); // 把城市名称放入 districtStrs
//                    }
//                    dts.add(districtStrs);
//
//
//                    mDistricts.add(dts);
//                }
//
//                mCities.add(cityStrs); // 组装城市数据
//
//            }
//        }
//
//
//    }

}
